$(function(){
	// 获取参数
	var topUtil = cmedit.global;
	var mainGuid = "";
	var tenantId = "";
	var token = window.localStorage.getItem("token");
	try{
		//发布预览
		var url = window.parent.document.getElementById("previewIframe").contentWindow.location.search
		mainGuid = topUtil.getParameterFromByUrl("mainGuid",url);
		tenantId = topUtil.getParameterFromByUrl("tenantId",url);
	}catch(e){
		//通过微信二维码预览
		mainGuid = topUtil.getParameterFromUrl("mainGuid");
		tenantId = topUtil.getParameterFromUrl("tenantId");
	}
	$.ajax({
		type: "post",
		async: true,
		dataType: "json",
		url: cmedit.global.cmsPath + "CmsController/getWebManuscriptByGuid",
		data: {
			"mainGuid" : mainGuid,
			"tenantId" : tenantId
		},
        beforeSend: function(XHR) {
            XHR.setRequestHeader("tenantId", tenantId);
            XHR.setRequestHeader("token", token);
        },
		success: function (data) {
			if (data.status) {
				// 渲染稿件信息
				var manuscript = data.data;
				$("#article_title").html(manuscript.mainHeader);
				$("#author").html(manuscript.mainCreateUserName); 
				$("#columnName").html(cmedit.global.isNotEmpty(manuscript.mainColumnName) ? manuscript.mainColumnName : "无"); 
				$("#createtime").html(manuscript.mainCreateTime);
				$("#totalWords").html(manuscript.mainTextContent.length);
				$("#webContent").html(manuscript.cmsH5Content);
				if(manuscript.mainIsOriginal == 1){
					$("#mainSources").html("原创");
				}else{
					$("#mainSources").html(cmedit.global.isNotEmpty(manuscript.mainSources) ? manuscript.mainSources : "转载");	
				}
				if(cmedit.global.isNotEmpty(manuscript.cmsSourceUrl)){
					$(".original-title").css('display','block');
					$("#sourceUrl").html(manuscript.cmsSourceUrl);
					$("#sourceUrl").attr("href",manuscript.cmsSourceUrl);
				}
				$("#article_content").html(manuscript.cmsH5Content);
				// 对稿件中的图片进行大小格式化
				var screenWidth = $(window).width();
				var screenHeight = $(window).height();
				$('#article_content img').load(function(){
					setWidthHeight(this,screenWidth-30);
				});
				$('#article_content video').css("width","80%").css("height","80%");
				$('#article_content img').error(function(){
					getFail(this);
				});
				if(screenWidth<500){
					$('html').css('width',screenWidth);
					$('body').css('width',screenWidth);
					$('#contentDiv').css('width',(screenWidth*0.92)+'px');
					$('#contentDiv').css('height',screenHeight+'px');
					$('.h1Class').css('font-size','23px');
					$('#webContent').css('font-size','16px');
					$('#webContent').css('word-break','break-word');
					$('#webContent').addClass("vid-wrapperOther");
				}
			} else {
				console.log(data);
			}
		}
	});
	

		function setWidthHeight(ImgD, screenWidth) {// 参数(图片,允许的宽度,允许的高度)
			var iwidth = 970;
			var iheight = 2000;
			var src = $(ImgD).attr("src");
			var imgwidth = $(ImgD).outerWidth();
			var imgheight = $(ImgD).outerHeight();
			if(imgwidth>0 && imgheight>0){
//     		   if(imgwidth/imgheight>= iwidth/iheight){
//     			   if(imgwidth>iwidth){
//     				   $(ImgD).attr("width", iwidth+"px");
//     				   $(ImgD).attr("height",((imgheight*iwidth)/imgwidth)+"px");
//     			   }else{
//     				   $(ImgD).attr("width", imgwidth+"px");
//     				   $(ImgD).attr("height", imgheight+"px");
//     			   }
//     		   }else{
//     			   if(imgheight>iheight){
//     				   $(ImgD).attr("width", ((imgwidth*iheight)/imgheight)+"px");
//     				   $(ImgD).attr("height", iheight+"px");
//     			   }else{
//     				   $(ImgD).attr("width", imgwidth+"px");
//     				   $(ImgD).attr("height", imgheight+"px");
//     			   }
//     		   }
				 $(ImgD).css("width", "80%");
				 $(ImgD).css("height",(imgheight/imgwidth)*0.8+"%");
     	   }
		}
		

	/**
	 * 图片加载失败方法
	 * 
	 * @param imgObj
	 */
	function getFail(imgObj, manuscripttype) {
		var screenWidth = $(window).width();
		$(imgObj).attr('alt', '');
		if (screenWidth < 500) {
			$(imgObj).css('width', screenWidth * 0.92);
			$(imgObj).css('height', screenWidth * 0.92);
		} else {
			if (manuscripttype == 1) {
				$(imgObj).css('width', '100%');
				$(imgObj).css('height', $(imgObj).outerWidth());
			} else {
				$(imgObj).css('width', '440px');
				$(imgObj).css('height', '440px');
			}
		}
	}
});